
#ifndef TOOLS_CONNECTION
#define TOOLS_CONNECTION

#ifndef TOOLS_UTIL
#include "define.h"
#endif

#include <cstdint>
#include <memory>
#include <functional>

class Server;
class Connection : public std::enable_shared_from_this<Connection>
{
public:
    Connection(Loop * loop, Server * server, std::shared_ptr<TcpSocket> socket);
    ~Connection();
    std::string PeerAddress();
    uint16_t    PeerPort();
    int64_t     Index();
    void        Close();
    int         SendData(const uint8_t * data, int length);
private:
    void ReadAble(std::shared_ptr<Channel> channel, std::shared_ptr<TcpSocket> socket);
    void WriteAble(std::shared_ptr<Channel> channel,std::shared_ptr<TcpSocket> socket);
    void ErrorAble(std::shared_ptr<Channel> channel,std::shared_ptr<TcpSocket> socket);
private:
    Loop *                     m_loop;
    Server *                   m_server;
    std::shared_ptr<TcpSocket> m_socket;
    std::shared_ptr<Channel>   m_channel;
    std::shared_ptr<SocketBuffer> m_socket_buffer;
};

#endif
